package com.library_management.dao.impl;

import com.library_management.dao.BookDao;
import com.library_management.model.Book;
import com.library_management.jdbc.JdbcUtil;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

public class BookDaoImpl implements BookDao {

    @Override
    public Book selectById(Integer id) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        Book book = null;

        try {
            conn = JdbcUtil.getConnection();
            String sql = "SELECT id, bookname, author, isbn, publisher, publish_date, stock, available FROM book WHERE id = ?";
            pstmt = conn.prepareStatement(sql);
            pstmt.setInt(1, id);
            rs = pstmt.executeQuery();

            if (rs.next()) {
                book = new Book();
                book.setId(rs.getInt("id"));
                book.setBookname(rs.getString("bookname"));
                book.setAuthor(rs.getString("author"));
                book.setIsbn(rs.getString("isbn"));
                book.setPublisher(rs.getString("publisher"));
                book.setPublishDate(rs.getDate("publish_date"));
                book.setStock(rs.getInt("stock"));
                book.setAvailable(rs.getInt("available"));
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(rs, pstmt, conn);
        }

        return book;
    }

    @Override
    public List<Book> selectByCondition(String bookname, String author) {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Book> books = new ArrayList<>();

        // 构建动态 SQL 条件
        StringBuilder sqlBuilder = new StringBuilder("SELECT id, bookname, author, isbn, publisher, publish_date, stock, available FROM book WHERE 1=1");
        List<String> params = new ArrayList<>();

        if (bookname != null && !bookname.trim().isEmpty()) {
            sqlBuilder.append(" AND bookname LIKE ?");
            params.add("%" + bookname + "%");
        }
        if (author != null && !author.trim().isEmpty()) {
            sqlBuilder.append(" AND author LIKE ?");
            params.add("%" + author + "%");
        }

        String sql = sqlBuilder.toString();
        try {
            conn = JdbcUtil.getConnection();
            pstmt = conn.prepareStatement(sql);

            // 设置参数
            for (int i = 0; i < params.size(); i++) {
                pstmt.setString(i + 1, params.get(i));
            }

            rs = pstmt.executeQuery();

            while (rs.next()) {
                Book book = new Book();
                book.setId(rs.getInt("id"));
                book.setBookname(rs.getString("bookname"));
                book.setAuthor(rs.getString("author"));
                book.setIsbn(rs.getString("isbn"));
                book.setPublisher(rs.getString("publisher"));
                book.setPublishDate(rs.getDate("publish_date"));
                book.setStock(rs.getInt("stock"));
                book.setAvailable(rs.getInt("available"));
                books.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(rs, pstmt, conn);
        }

        return books;
    }

    @Override
    public List<Book> selectAvailableBooks() {
        Connection conn = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        List<Book> books = new ArrayList<>();

        try {
            conn = JdbcUtil.getConnection();
            String sql = "SELECT id, bookname, author, isbn, publisher, publish_date, stock, available FROM book WHERE available > 0";
            pstmt = conn.prepareStatement(sql);
            rs = pstmt.executeQuery();

            while (rs.next()) {
                Book book = new Book();
                book.setId(rs.getInt("id"));
                book.setBookname(rs.getString("bookname"));
                book.setAuthor(rs.getString("author"));
                book.setIsbn(rs.getString("isbn"));
                book.setPublisher(rs.getString("publisher"));
                book.setPublishDate(rs.getDate("publish_date"));
                book.setStock(rs.getInt("stock"));
                book.setAvailable(rs.getInt("available"));
                books.add(book);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        } finally {
            JdbcUtil.close(rs, pstmt, conn);
        }

        return books;
    }
}
